package com.ykdl.member.kid.adapters;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ykdl.member.kid.R;
import com.ykdl.member.KidApp;
import com.ykdl.member.kid.beans.BabyProfileBean;
import com.ykdl.member.kid.beans.BodyTypeBean;
import com.ykdl.member.kid.beans.FeedReportDayBean;
import com.ykdl.member.kid.beans.ProfileBean;
import com.ykdl.member.kid.util.DateUtil;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
/**
 * 
 * @author Pengfei
 *
 */
public class FeedReportDayAdapter extends BaseAdapter {
	
	private Context context;
	private List<FeedReportDayBean> beans = new ArrayList<FeedReportDayBean>();
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	
	public FeedReportDayAdapter(Context context){
		this.context = context;
	}
	
	public List<FeedReportDayBean> getBeans() {
		return beans;
	}

	public void addBeans(List<FeedReportDayBean> beans) {
		if (beans != null && beans.size() != 0) {
			for (FeedReportDayBean b : beans) {
				this.beans.add(b);
			}
		}
	}
	
	public void cleanData(){
		this.beans.clear();
	}

	@Override
	public int getCount() {
		if (beans == null) {
			return 0;
		} else {
			return beans.size();
		}
	}

	@Override
	public Object getItem(int position) {
		return beans.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.item_feed_report, null);
			holder.txtDate = (TextView)convertView.findViewById(R.id.txtDate);
			holder.txtXuxu = (TextView)convertView.findViewById(R.id.txtXuxu);
			holder.txtBianBian = (TextView)convertView.findViewById(R.id.txtBianBian);
			holder.txtXuBian = (TextView)convertView.findViewById(R.id.txtXuBian);
			holder.txtSleep = (TextView)convertView.findViewById(R.id.txtSleep);
			holder.txtFeeder = (TextView)convertView.findViewById(R.id.txtFeeder);
			holder.txtAmount = (TextView)convertView.findViewById(R.id.txtFeederAmount);
			holder.txtLeft = (TextView)convertView.findViewById(R.id.txtLeft);
			holder.txtLeftDuretion = (TextView)convertView.findViewById(R.id.txtLeftDuretion);
			holder.txtRight = (TextView)convertView.findViewById(R.id.txtRight);
			holder.txtRightDuretion = (TextView)convertView.findViewById(R.id.txtRightDuretion);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		FeedReportDayBean bean = (FeedReportDayBean) getItem(position);
		Date d = new Date((long) bean.getDate() * 1000);
		holder.txtDate.setText(sdf.format(d));
		holder.txtXuxu.setText(String.valueOf(bean.getDefecate_count())+"次");
		holder.txtBianBian.setText(String.valueOf(bean.getDefecate_count())+"次");
		holder.txtXuBian.setText(String.valueOf(bean.getDefecate_count())+"次");
		holder.txtSleep.setText(String.valueOf(bean.getSleep_count())+"次");
		holder.txtFeeder.setText(String.valueOf(bean.getFeed_formula_count())+"次");
		holder.txtAmount.setText(String.valueOf(bean.getFeed_formula_amount())+"ml");
		holder.txtLeft.setText(String.valueOf(bean.getFeed_left_breast_count())+"次");
		holder.txtLeftDuretion.setText(formatTime(bean.getFeed_left_breast_time_amount()));
		holder.txtRight.setText(String.valueOf(bean.getFeed_right_breast_count())+"次");
		holder.txtRightDuretion.setText(formatTime(bean.getFeed_right_breast_time_amount()));
		return convertView;
	}
	
	private String formatTime(int duration){
		int h = duration / 60;
		int m = duration % 60;
		if (h != 0 && m != 0) {
			return h+"小时"+m+"分钟";
		} else {
			if (h == 0) {
				return m+"分钟";
			} else if (m == 0) {
				return h+"小时";
			} else {
				return "";
			}
		}
	}
	
	class ViewHolder {
		TextView txtDate;
		TextView txtXuxu, txtBianBian, txtXuBian;
		TextView txtSleep;
		TextView txtFeeder,txtAmount;
		TextView txtLeft, txtLeftDuretion;
		TextView txtRight, txtRightDuretion;
	}
}
